<?php

class Attach_lib {
    var $CI;      
    
    function Attach_lib() {
	$this->CI =& get_instance();	
    }
    
    
    function addImage($mailer_id) {
	    
	$path_full= $this->CI->config->item('upload_path_full');
	$path_thumb= $this->CI->config->item('upload_path_thumb');
	
	$response=array("success"=>false,"message"=>"Probleme la upload");
	
	$field_name = "upload";
	$config['upload_path']= $path_full;	
	$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
	$config['remove_spaces']  =TRUE;
	$config['overwrite'] = FALSE;		
			
	$this->CI->upload->initialize($config); 

	if ( ! $this->CI->upload->do_upload($field_name)) {
		//$response = array('error' => $this->CI->upload->display_errors());
		return $response;			
	} else {
		$data = $this->CI->upload->data();			
		$file_name=$data['file_name'];		
		 $config = array(     
			    array(
				'image_library' => 'GD2',
				'source_image'    => $path_full.$file_name,
				'create_thumb'    => TRUE,
				'new_image'=> $path_thumb.$file_name,
				'maintain_ratio'=> TRUE,
				'thumb_marker'    => '',
				'width'            => 100,
				'height'        => 100
			    ));

		foreach ($config as $item)	{			    
		    $this->CI->image_lib->initialize($item);
		    if(!$this->CI->image_lib->resize()) {
			return $response;			
		    }			    
		    $this->CI->image_lib->clear();
		}
		$result=$this->CI->model_mailer->addAttach($file_name,$mailer_id,"image");
		$response=array("success"=>true,"message"=>"Upload reusit!");
		if ($result)			
		return $response;			
	}
    }
    
    function addAttach($mailer_id) {
		
		$path_full= $this->CI->config->item('upload_path_attach');		
		$response=array("success"=>false,"message"=>"Probleme la upload");		
		$field_name = "upload";		
		
		$config['upload_path']= $path_full;	
		$config['allowed_types'] = '*';		
		$config['remove_spaces']  =TRUE;
		$config['overwrite'] = FALSE;		
		
		$this->CI->load->library('upload', $config);		
		$this->CI->upload->initialize($config); 	
			
		if ( ! $this->CI->upload->do_upload($field_name)) {
			 $this->CI->upload->display_errors('<p>', '</p>');
			return $response;			
		} else {
			$data = $this->CI->upload->data();			
			$file_name=$data['file_name'];	
			
			$result=$this->CI->model_mailer->addAttach($file_name,$mailer_id,"attach");
			$response=array("success"=>true,"message"=>"Upload reusit!");
			return $response;			
		}
	}
    

}

 ?>